package edu.zhang.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.zhang.dao.AdminDAO;
import edu.zhang.dao.NoticeDAO;
import edu.zhang.po.Admin;
import edu.zhang.po.Notice;

@WebServlet("/admin/AdminController")
public class AdminController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminDAO adminDAO = new AdminDAO();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String option = request.getParameter("option");
		
		if(option.equals("list")) {
			List<Admin> list = adminDAO.getAllAdmin();
			if(list!=null) {
				request.setAttribute("list", list);
				request.getRequestDispatcher("/Back/admin/listAdmin.jsp").forward(request, response);
			}else {
				response.sendRedirect("error.jsp");
			}
		}else if(option.equals("insert")) {
			String account = request.getParameter("account");
			String password = request.getParameter("password");
			
			Admin admin = new Admin();
			admin.setAccount(account);
			admin.setPassword(password);
			
			int i = adminDAO.insert(admin);
			if (i > 0) {
				request.getRequestDispatcher("AdminController?option=list").forward(request, response);
			} else {
				response.sendRedirect("/Back/admin/error.jsp");
			}
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
